//@author A0105536L
package Logic.LogicUnitTest;

import static org.junit.Assert.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import org.junit.Test;

import com.joestelmach.natty.Parser;

import Logic.DSQueryFactory;
import Object.DSException;
import Object.DSQuery;
/**********************************************************************************
 * Class DSQueryFactoryTest. This class is to do J-Unit test for DSQueryFactory
 *
 *
 */

public class DSQueryFactoryTest {
	DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy",Locale.ENGLISH); 
	Parser nattyParser = new Parser();
	@Test
	public void test_Tags() throws ParseException, DSException {
		DSQueryFactory qf = new DSQueryFactory();
		DSQuery q = new DSQuery();
		q.addTag("tag1");
		q.addTag("tag2");
		q.addTag("test");
		// To test tag.
		assertEquals(q.getTags(),qf.createQuery("test tag tag1 tag2 test").getTags()); 
	}

	@Test
	public void test_Keywords() throws ParseException, DSException {
		DSQueryFactory qf = new DSQueryFactory();
		DSQuery q = new DSQuery();
		q.addKeyWord("keyword");
		q.addKeyWord("test");
		// To test keyword
		assertEquals(q.getKeyWords(),qf.createQuery("keyword test").getKeyWords()); 
	}
	
	@Test
	public void test_endDate() throws ParseException, DSException {
		DSQueryFactory qf = new DSQueryFactory();
		DSQuery q = new DSQuery();
		
		q.setEndDate(nattyParser.parse("mar 3").get(0).getDates().get(0));
		// To test after an end date
		assertEquals(q.getEndDate().toString(),qf.createQuery("after 3 mar").getEndDate().toString());
	}
	@Test
	public void test_DateRange() throws ParseException, DSException {
		DSQueryFactory qf = new DSQueryFactory();
		DSQuery q = new DSQuery();
		q.setStartDate(nattyParser.parse("mar 3").get(0).getDates().get(0));
		q.setEndDate(nattyParser.parse("mar 13").get(0).getDates().get(0));
		q.setDateRange(4);
		// To test date range variable for "after"
		assertEquals(q.getDateRange(),qf.createQuery("after 3 mar").getDateRange());
	}
	
	@Test
	public void test_DateRange_And_Duration() throws ParseException, DSException {
		DSQueryFactory qf = new DSQueryFactory();
		DSQuery q = new DSQuery();
		q.setStartDate(nattyParser.parse("mar 3").get(0).getDates().get(0));
		q.setEndDate(nattyParser.parse("mar 13").get(0).getDates().get(0));
		q.setDateRange(5);
		// to test during: start date 
		assertEquals(q.getStartDate().toString(),qf.createQuery("from mar 3 to mar 13").getStartDate().toString());
		// to test during: end date
		assertEquals(q.getEndDate().toString(),qf.createQuery("from mar 3 to mar 13").getEndDate().toString());
		// to test the date range variable for "during"
		assertEquals(q.getDateRange(),qf.createQuery("from mar 3 to mar 13").getDateRange());
	}
	@Test
	public void test_Multiple() throws ParseException, DSException {
		DSQueryFactory qf = new DSQueryFactory();
		DSQuery test = new DSQuery();
		DSQuery q = new DSQuery();
		q.addTag("2103");
		q.addTag("tut");
		q.addKeyWord("meeting");
		q.setEndDate(nattyParser.parse("mar 3").get(0).getDates().get(0));
		q.setDateRange(4);
		test = qf.createQuery("meeting after mar 3 tag 2103 tut");
		// Multiple criteria for searching.
		// to test after a end date
		assertEquals(q.getEndDate().toString(),test.getEndDate().toString());
		// to test date range variable for "after"
		assertEquals(q.getDateRange(),test.getDateRange());
		// to test tags
		assertEquals(q.getTags(),test.getTags());
		// to test keywords     Noted: there is "after" in keyword also, to test whether it is correct or not
		assertEquals(q.getKeyWords(),test.getKeyWords());
		
	}
}
